package com.campus.secondhand.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.secondhand.common.Result;
import com.campus.secondhand.entity.ProductCategories;
import com.campus.secondhand.mapper.ProductCategoryMapper;
import com.campus.secondhand.model.vo.CategoryTreeVO;
import com.campus.secondhand.model.vo.CategoryVO;
import com.campus.secondhand.service.CategoryService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 商品分类服务实现类
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategories>
        implements CategoryService {

    private final RedisTemplate<String, Object> redisTemplate;

    // Redis缓存键常量
    private static final String CACHE_ALL_CATEGORIES = "category:all";
    private static final long CACHE_EXPIRATION = 24; // 缓存过期时间，单位小时

    /**
     * 获取分类名称
     */
    @Override
    public String getCategoryName(Long categoryId) {
        if (categoryId == null) {
            return null;
        }

        // 缓存未命中，从数据库获取
        ProductCategories category = getById(categoryId);
        String name = category != null ? category.getName() : null;
        return name;
    }

    /**
     * 获取所有分类
     */
    @Override
    public Result<List<CategoryVO>> getAllCategories() {
        // 尝试从缓存获取
        Object cachedCategories = redisTemplate.opsForValue().get(CACHE_ALL_CATEGORIES);
        if (cachedCategories != null) {
            return Result.success((List<CategoryVO>) cachedCategories, "获取成功(缓存)");
        }

        // 缓存未命中，从数据库获取
        LambdaQueryWrapper<ProductCategories> queryWrapper = Wrappers.lambdaQuery(ProductCategories.class)
                .eq(ProductCategories::getStatus, 1)
                .orderByAsc(ProductCategories::getSortOrder);

        List<ProductCategories> categories = list(queryWrapper);
        List<CategoryVO> vos = categories.stream().map(this::convertToVO).collect(Collectors.toList());

        // 写入缓存
        redisTemplate.opsForValue().set(CACHE_ALL_CATEGORIES, vos, CACHE_EXPIRATION, TimeUnit.HOURS);

        return Result.success(vos, "获取成功");
    }

    /**
     * 将ProductCategory实体转换为CategoryVO
     */
    private CategoryVO convertToVO(ProductCategories category) {
        CategoryVO vo = new CategoryVO();
        BeanUtils.copyProperties(category, vo);
        return vo;
    }

    /**
     * 将ProductCategory实体转换为CategoryTreeVO
     */
    private CategoryTreeVO convertToTreeVO(ProductCategories category) {
        CategoryTreeVO vo = new CategoryTreeVO();
        BeanUtils.copyProperties(category, vo);
        return vo;
    }


}